package com.pinc.wacow;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.pinc.wacow.R;

public class Question_Content extends FlipActivity{
	private int[] mIdArr;
	private String[] mQuestion;
	private int[] mImgID;
	private String[][] mSelection;
	private boolean[][] mAnswer;
	private boolean mHideAnswer;
	private DBClass dbHelper;
	private TextView tv_percent;
	private TextView tv_last;
	private TextView tv_lresult;
	private int[] mResult;
	private String[] mPercent;
	private String[] mLast;
	private String[] mFrom;
	private ProgressDialog progressDialog;
	private ShowTask mTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_flip);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		initWidget();
		cur_qstn = this.getIntent().getIntExtra("pos", 0);
		mIdArr = this.getIntent().getIntArrayExtra("ids");
		mHideAnswer = true;

		dbHelper = new DBClass(this);
		mTask = new ShowTask();
		mTask.execute();
	}

	private void getQuestions()
	{
		int qstn_total = mIdArr.length;
		String[] s = new String[GParam.SELECT_MAX];

		mQuestion = new String[qstn_total];
		mImgID = new int[qstn_total];
		mSelection = new String[qstn_total][];
		mAnswer = new boolean[qstn_total][];
		mResult = new int[qstn_total];
		mPercent = new String[qstn_total];
		mLast = new String[qstn_total];
		mFrom = new String[qstn_total];

		for ( int i=0; i<qstn_total;i++)
		{
			Cursor cs = dbHelper.getRecord(DBClass.TABLE_NAME_QLIB, "_id = ?", new String[]{String.valueOf(mIdArr[i])}, null, null);
			cs.moveToFirst();

			int selCount = cs.getInt(cs.getColumnIndex(DBClass.COL_QLIB_SEL_TOTAL));
			mSelection[i] = new String[selCount]; 
			String answer_str = cs.getString(cs.getColumnIndex(DBClass.COL_QLIB_ANS)); 
			mAnswer[i] =  Test_Setting.AnsStr2Arr(answer_str);
			mImgID[i] = cs.getInt(cs.getColumnIndex(DBClass.COL_QLIB_IMAGE));
			mQuestion[i] = cs.getString(cs.getColumnIndex(DBClass.COL_QLIB_QUESTION));
			mResult[i] = cs.getInt(cs.getColumnIndex(DBClass.COL_QLIB_LAST_RESULT));
			mPercent[i] = Question_Adapter.getPercentage(cs.getInt(cs.getColumnIndex(DBClass.COL_QLIB_BINGO_NO)), cs.getInt(cs.getColumnIndex(DBClass.COL_QLIB_TESTED_NO)))+String.format(" (%d/%d)", cs.getInt(cs.getColumnIndex(DBClass.COL_QLIB_BINGO_NO)), cs.getInt(cs.getColumnIndex(DBClass.COL_QLIB_TESTED_NO)));
			mLast[i] = Question_Adapter.getLastUpdate( cs.getString(cs.getColumnIndex(DBClass.COL_QLIB_LAST_TEST)));
			mFrom[i] = Category_Name.getCategoryName(cs.getInt(cs.getColumnIndex(DBClass.COL_QLIB_CATEGORY))); 

			s[0] = cs.getString(cs.getColumnIndex(DBClass.COL_QLIB_SEL1));
			s[1] = cs.getString(cs.getColumnIndex(DBClass.COL_QLIB_SEL2));
			s[2] = cs.getString(cs.getColumnIndex(DBClass.COL_QLIB_SEL3));
			s[3] = cs.getString(cs.getColumnIndex(DBClass.COL_QLIB_SEL4));
			s[4] = cs.getString(cs.getColumnIndex(DBClass.COL_QLIB_SEL5));

			for ( int j = 0; j< selCount; j++)
			{
				mSelection[i][j] = s[j];
			}

			cs.close();
			dbHelper.mDB.close();
		}
	}

	private class ShowTask extends AsyncTask<Context, Void, Integer> {
		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(Question_Content.this);
			progressDialog.setMessage(getResources().getString(R.string.str_loading));
			progressDialog.setCancelable(false);
			progressDialog.show();
		}

		protected Integer doInBackground(Context... ctx) {
			getQuestions();
			return 0;
		}

		@Override
		protected void onPostExecute(Integer err) {
			showQuestion();
			progressDialog.cancel();
		}
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		/* set upbar */
		tv_title.setText(getIntent().getStringExtra("title"));
		b_next.setVisibility(View.VISIBLE);
		b_next.setText(getResources().getString(R.string.str_show_answer));
		b_next.setOnClickListener(ShowListener);
		b_prev.setVisibility(View.VISIBLE);
		b_prev.setText(getResources().getString(R.string.str_back));
		b_prev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		/* set widget */
		Widget_Window ww = (Widget_Window)this.findViewById(R.id.widgetWindow1);
		ww.init(getResources().getString(R.string.str_record), new String[]{getResources().getString(R.string.str_correct_ratio),getResources().getString(R.string.str_last_test),getResources().getString(R.string.str_last_result)});

		tv_percent = ww.getValueTextViews()[0];
		tv_last = ww.getValueTextViews()[1];
		tv_lresult = ww.getValueTextViews()[2];
	}

	@Override
	protected void onDestroy() {
		mQuestion = null;
		mImgID = null;
		mSelection = null;
		mAnswer = null;
		mResult = null;
		mPercent = null;
		mLast = null;
		mFrom = null;
		if (mTask != null)
			mTask.cancel(true);
		super.onDestroy();
	}

	private View.OnClickListener HideListener = new View.OnClickListener(){
		@Override
		public void onClick(View v) {
			mHideAnswer = true;
			b_next.setText(getResources().getString(R.string.str_show_answer));
			b_next.setOnClickListener(ShowListener);
			flipper.setInAnimation(null);
			flipper.setOutAnimation(null);
			showQuestion();
			flipper.setDisplayedChild(0);
		}
	};

	private View.OnClickListener ShowListener = new View.OnClickListener(){
		@Override
		public void onClick(View v) {
			mHideAnswer = false;
			b_next.setText(getResources().getString(R.string.str_hide_answer));
			b_next.setOnClickListener(HideListener);
			flipper.setInAnimation(null);
			flipper.setOutAnimation(null);
			showQuestion();
			flipper.setDisplayedChild(0);
		}
	};

	@Override
	protected String getQuestion(int qstn_no) {
		return mQuestion[qstn_no];
	}

	@Override
	protected String[] getSelections(int qstn_no) {
		return mSelection[qstn_no];
	}

	@Override
	protected String getCategoryName(int qstn_no) {
		return "("+ mFrom[qstn_no] +")";
	}

	@Override
	protected void markSelection(TextView[] tv_sel, int qstn_no) {
		for ( int i = 0; i< GParam.SELECT_MAX; i++)
		{
			if ( !mHideAnswer && mAnswer[qstn_no][i])
				tv_sel[i].setTextColor(Color.RED);
			else
				tv_sel[i].setTextColor(Color.BLACK);
		}
	}

	@Override
	protected int getQuestionCnt() {
		return mIdArr.length;
	}

	@Override
	protected void postHandle(int no) {
		switch(mResult[no])
		{
		case 0:
			tv_lresult.setText(getResources().getString(R.string.str_get_wrong));
			tv_lresult.setTextColor(getResources().getColor(R.color.history_wrong));
			break;
		case 1:
			tv_lresult.setText(getResources().getString(R.string.str_get_correct));
			tv_lresult.setTextColor(getResources().getColor(R.color.history_right));
			break;
		default:
			tv_lresult.setText("--");
			tv_lresult.setTextColor(Color.WHITE);
			break;
		}
		tv_percent.setText(mPercent[no]);
		tv_last.setText(mLast[no]);
		tv_title.setTextSize(16);
		tv_title.setText(String.format("%s\n(%d/%d)", getIntent().getStringExtra("title"),no+1,mIdArr.length));
	}

	@Override
	protected int getImageIndex(int qstn_no) {
		return mImgID[qstn_no];
	}

}
